﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;

namespace ITAMngApp.TkMng.TaskForm
{
    public partial class EditorForm : DockContent
    {
        private GMngMainForm m_ParentForm = null;

        private string m_fileName = "";

        static int st = 0;
        static string fs = "";

        public void set_file_name(string fn)
        {
            m_fileName = fn;
        }

        public string get_file_name()
        {
            return m_fileName;
        }

        public EditorForm(GMngMainForm mainForm, string fileName)
        {
            InitializeComponent();
            this.m_ParentForm = mainForm;

            openFileInBox(this.editorAreaTextBox, fileName);
            this.Text = fileName;
            base.ShowHint = DockState.Document;

        }

        public EditorForm(GMngMainForm mainForm)
        {
            InitializeComponent();
            this.m_ParentForm = mainForm;
            base.ShowHint = DockState.Document;
        }

        public void save(string file_name)
        {
            if (file_name == "")
            {
                editorAreaTextBox.SaveFile(this.m_fileName, RichTextBoxStreamType.TextTextOleObjs);
                this.Text = this.m_fileName;
            }
            else
            {
                this.m_fileName = file_name;
                editorAreaTextBox.SaveFile(this.m_fileName, RichTextBoxStreamType.TextTextOleObjs);
                this.Text = this.m_fileName;
            }
        }

        public void saveAs(string fileName)
        {
            this.m_fileName = fileName;
            editorAreaTextBox.SaveFile(this.m_fileName, RichTextBoxStreamType.TextTextOleObjs);
            this.Text = this.m_fileName;
        }

        public void redo()
        {
            if(editorAreaTextBox.CanRedo)
                editorAreaTextBox.Redo();
        }

        public void undo()
        {
            if (editorAreaTextBox.CanUndo)
                editorAreaTextBox.Undo();
        }

        public void cut()
        {
            editorAreaTextBox.Cut();
        }

        public void paste()
        {
            editorAreaTextBox.Paste();
        }

        public void copy()
        {
            editorAreaTextBox.Copy();
        }

        public void selectAll()
        {
            editorAreaTextBox.SelectAll();
        }

        public void search(string s)
        {
            if (s != fs)
            {
                fs = s;
                st = 0;
            }

            int now = this.editorAreaTextBox.Find(s, st, this.editorAreaTextBox.Text.Length, RichTextBoxFinds.NoHighlight);
            if(now > 0)
            {
                this.editorAreaTextBox.Select(now,s.Length);
                this.Focus();
                st = now + s.Length;
            }
            else
            {
                st = 0;
                MessageBox.Show("已搜索到结尾，或者没有找到相应字符");
            }       
        }

        public void printPreview()
        {
            printPreviewDialog1.Document = this.printDocument1;
            printPreviewDialog1.ShowDialog();
        }

        public void print()
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
                printDocument1.Print();
        }

        private void openFileInBox(RichTextBox owner, string fileName)
        {
            StreamReader mStreamReader = new StreamReader(fileName, System.Text.Encoding.Default);

            try
            {
                //使用StreamReader类来读取文件
                mStreamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                // 从数据流中读取每一行，直到文件的最后一行，并在richTextBox1中显示出内容
                owner.Text = "";
                string strLine = mStreamReader.ReadLine();
                while (strLine != null)
                {
                    this.editorAreaTextBox.Text += strLine + "\n";
                    strLine = mStreamReader.ReadLine();
                }
                m_fileName = fileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                mStreamReader.Close();
            }

        }

        #region  右键菜单事件响应函数
        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            paste();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            selectAll();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            redo();
        }
        #endregion
    }
}
